package com.easthome.controller;

import com.easthome.utils.RedisConnectionPool;
import redis.clients.jedis.Jedis;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 校验验证码
 */
@WebServlet(urlPatterns = "/checkCode")
public class CheckCodeServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("text/html;charset=utf-8");
        String tel =req.getParameter("tel");
        String code =req.getParameter("code");
        //手机号组成redis数据库中键
        String key=tel+"::code";
        Jedis jedis= RedisConnectionPool.getJedis();
        String redisCode = jedis.get(key);
        if (redisCode == null){
            resp.getWriter().println("验证码已过期");
        }else if (!code.equalsIgnoreCase(redisCode)){
            resp.getWriter().println("验证码错误");
        }else {
            resp.getWriter().println("验证码正确");
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

    }
}

